package br.jonasLab.financistoweb.control;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import br.jonasLab.financistoweb.persistence.ExtratoAplicMesAnoDAO;
import br.jonasLab.financistoweb.utils.Utils;
import br.jonasLab.financistoweb.vo.ExtratoAplicMesAnoVO;

public class ExtratosControl implements IExtratosControl {
		
	@Override
	public List<ExtratoAplicMesAnoVO> extratoMesAnoFgts() throws Exception {
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		ExtratoAplicMesAnoDAO dao = new ExtratoAplicMesAnoDAO();
		List<ExtratoAplicMesAnoVO> listRetorno = dao.extratoMesAnoFgts();
		if (listRetorno != null){
			for (int i = 0; i < listRetorno.size(); i++) {
				calcularValores(listRetorno.get(i),saldoInicial,saldoFinal);
				saldoInicial = listRetorno.get(i).getSaldoInicial();
				saldoFinal = listRetorno.get(i).getSaldoFinal();
			}
		}
		return listRetorno;
	}
	
	@Override
	public List<ExtratoAplicMesAnoVO> extratoAplicacoes() throws Exception {
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		ExtratoAplicMesAnoDAO dao = new ExtratoAplicMesAnoDAO();
		List<ExtratoAplicMesAnoVO> listRetorno = dao.extratoAplicacoes();
		if (listRetorno != null){
			for (int i = 0; i < listRetorno.size(); i++) {
				calcularValores(listRetorno.get(i),saldoInicial,saldoFinal);
				saldoInicial = listRetorno.get(i).getSaldoInicial();
				saldoFinal = listRetorno.get(i).getSaldoFinal();
			}
		}
		return listRetorno;
	}
	
	@Override
	public List<ExtratoAplicMesAnoVO> extratoIlka() throws Exception {
		int idx = 0;
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		List<ExtratoAplicMesAnoVO> listRetorno = new ArrayList<ExtratoAplicMesAnoVO>();
		for (int i = 0; i < 5; i++) {
			listRetorno.add(calcularValores(i,saldoInicial,saldoFinal));
			saldoInicial = listRetorno.get(i).getSaldoInicial();
			saldoFinal = listRetorno.get(i).getSaldoFinal();
			idx++;
		}
		ExtratoAplicMesAnoDAO dao = new ExtratoAplicMesAnoDAO();
		HashMap<String, Double> listaTaxas = taxaMes();
		List<ExtratoAplicMesAnoVO> listaExtrato = dao.extratoIlka();
		for (int i = 0; i < listaExtrato.size(); i++) {
			if (i == 0){
				listaExtrato.get(i).setDeposito(0.0);
			}
			listRetorno.add(calcularValores(listaExtrato.get(i),listaTaxas.get(listaExtrato.get(i).getMesAno()),saldoInicial,saldoFinal));
			saldoInicial = listRetorno.get(i+idx).getSaldoInicial();
			saldoFinal = listRetorno.get(i+idx).getSaldoFinal();
		}
		return listRetorno;
	}
	
	public HashMap<String, Double> taxaMes() throws Exception{
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		HashMap<String, Double> retorno = new HashMap<String, Double>();
		ExtratoAplicMesAnoDAO dao = new ExtratoAplicMesAnoDAO();
		List<ExtratoAplicMesAnoVO> listRetorno = dao.extratoAplicacoes();
		if (listRetorno != null){
			for (int i = 0; i < listRetorno.size(); i++) {
				calcularValores(listRetorno.get(i),saldoInicial,saldoFinal);
				saldoInicial = listRetorno.get(i).getSaldoInicial();
				saldoFinal = listRetorno.get(i).getSaldoFinal();
				retorno.put(listRetorno.get(i).getMesAno(), listRetorno.get(i).getTaxa());
			}
		}
		return retorno;
	}
	
	private ExtratoAplicMesAnoVO calcularValores(ExtratoAplicMesAnoVO vo , Double saldoInicial , Double saldoFinal){
		saldoInicial = saldoFinal ;
		saldoFinal+= vo.getDeposito()+vo.getJuros()+vo.getSaque();
		if(saldoInicial != 0){
			vo.setTaxa(Utils.divideValores(vo.getJuros(), saldoInicial, 4));
		}else if(vo.getJuros()!=0){
			vo.setTaxa(Utils.divideValores(vo.getJuros(), vo.getDeposito(), 4));
		}else{
			vo.setTaxa(0.0);
		}
		vo.setSaldoInicial(saldoInicial);
		vo.setSaldoFinal(saldoFinal);
		return vo;
	}
	
	private ExtratoAplicMesAnoVO calcularValores(ExtratoAplicMesAnoVO vo, Double taxa , Double saldoInicial , Double saldoFinal){
		vo.setTaxa(taxa);
		saldoInicial =  saldoFinal;
		if(saldoInicial != 0){
			vo.setJuros(saldoInicial* taxa );
		}else{
			vo.setJuros(0.0);
		}
		saldoFinal+=vo.getDeposito()+vo.getJuros()+vo.getSaque();
		vo.setSaldoInicial(saldoInicial);
		vo.setSaldoFinal(new BigDecimal( saldoFinal ).setScale(2,RoundingMode.HALF_EVEN).doubleValue());
		return vo;
	}
	
	private ExtratoAplicMesAnoVO calcularValores(int ordem, Double saldoInicial , Double saldoFinal){
		ExtratoAplicMesAnoVO vo = new ExtratoAplicMesAnoVO();
		saldoInicial = saldoFinal;
		vo.setDeposito(250.00);
		vo.setTaxa(0.0057);
		vo.setSaque(0.0);
		if(ordem == 0){
			vo.setMesAno("07/2013");	
			vo.setJuros(0.0);
			vo.setTaxa(0.0);
		}else if(ordem == 1){
			vo.setMesAno("08/2013");
			vo.setJuros(saldoInicial*vo.getTaxa());
		}else if(ordem == 2){
			vo.setMesAno("09/2013");
			vo.setJuros(saldoInicial*vo.getTaxa());
		}else if(ordem == 3){
			vo.setMesAno("10/2013");
			vo.setJuros(saldoInicial*vo.getTaxa());
		}else if(ordem == 4){
			vo.setMesAno("11/2013");
			vo.setJuros(saldoInicial*vo.getTaxa());
		}	
		saldoFinal+=vo.getDeposito()+vo.getJuros()+vo.getSaque();
		vo.setSaldoInicial(saldoInicial);
		vo.setSaldoFinal(new BigDecimal(saldoFinal).setScale(2,RoundingMode.HALF_EVEN).doubleValue());
		return vo;
	}
}
